import { Text, View } from "@tarojs/components";
import { Avatar, Button, Cell } from "@nutui/nutui-react-taro";
import { getUserInfo, removeToken } from "@/utils/token";
import Taro from "@tarojs/taro";
import { useRequest } from "taro-hooks";
import { loginOut } from "@/services/User";
import styles from "./index.scss";
import { User } from "@nutui/icons-react-taro";
import { getImgUrl } from "@/utils";

const Index = () => {
  const { run } = useRequest(loginOut, {
    manual: true,
    onSuccess: () => {
      removeToken();
      Taro.reLaunch({ url: "/loginAndRegister/login/index" });
    },
  });
  const { nickname, avatar } = getUserInfo();

  return (
    <View className={styles.container}>
      <Avatar
        size="large"
        src={avatar ? getImgUrl(avatar) : void 0}
        icon={avatar ? void 0 : <User />}
      />
      <Text className={styles.username}>{nickname}</Text>
      <Cell
        onClick={() => {
          Taro.navigateTo({
            url: "/innerPages/editUserInfo/index",
          });
        }}
      >
        编辑个人信息
      </Cell>
      <Cell onClick={() => run()}>退出登录</Cell>
    </View>
  );
};

export default Index;
